from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from ui.Ui_devices_searcher_widget import Ui_widget_device_searcher



class Search_signal(QObject):
    search_device = Signal(dict)



class Device_searcher_widget(QDialog):
    
    
    def __init__(self, parent=None):
        super(Device_searcher_widget, self).__init__(parent)
        self.ui = Ui_widget_device_searcher()
        self.ui.setupUi(self)
        
        self.setModal(True)  # 设置窗体为模态窗口，必须为True
        self.setWindowFlags(Qt.WindowCloseButtonHint)  # 设置窗体仅显示关闭按钮

        self.signal = Search_signal()
        self.init_col_names()
        self.init_item()

        self.ui.pushButton_search.clicked.connect(self.search_device)

        self.ui.tableWidget.horizontalHeader().setStretchLastSection(True)  #设置最后一行自适应宽度
    def init_item(self):
        max_col = self.ui.tableWidget.columnCount()
        for i in range(max_col):
            self.ui.tableWidget.setItem(0,i, QTableWidgetItem(''))



    def init_col_names(self):
        self.col_names = {}
        # 获取列id
        max_col = self.ui.tableWidget.columnCount()
        for i in range(max_col):
            text = self.ui.tableWidget.horizontalHeaderItem(i).text()
            self.col_names[text] = i


    def search_device(self):
        filter = {
            'name' : self.ui.tableWidget.item(0,self.col_names['设备名']).text(),
            'ip' : self.ui.tableWidget.item(0,self.col_names['ip']).text(),
            'user' : self.ui.tableWidget.item(0,self.col_names['用户']).text(),
            'port' : self.ui.tableWidget.item(0,self.col_names['端口']).text(),
            'password' : self.ui.tableWidget.item(0,self.col_names['密码']).text(),
            'position' : self.ui.tableWidget.item(0,self.col_names['位置']).text(),
            'supplier' : self.ui.tableWidget.item(0,self.col_names['厂商']).text(),
            'model' : self.ui.tableWidget.item(0,self.col_names['型号']).text(),
            'info' : self.ui.tableWidget.item(0,self.col_names['描述']).text(),
        }
        # 逐行搜索 不符合则隐藏
        for row in range(self.ui.tableWidget.rowCount()):
            self.ui.tableWidget.hideRow(row)
            text = self.ui.tableWidget.item(row,self.col_names['设备名']).text()
            if text != '' and text.find(filter['name'])<0:
                continue
            text = self.ui.tableWidget.item(row,self.col_names['ip']).text()
            if text != '' and text.find(filter['ip'])<0:
                continue
            text = self.ui.tableWidget.item(row,self.col_names['用户']).text()
            if text != '' and text.find(filter['user'])<0:
                continue
            text = self.ui.tableWidget.item(row,self.col_names['端口']).text()
            if text != '' and text.find(filter['port'])<0:
                continue
            text = self.ui.tableWidget.item(row,self.col_names['密码']).text()
            if text != '' and text.find(filter['password'])<0:
                continue
            text = self.ui.tableWidget.item(row,self.col_names['位置']).text()
            if text != '' and text.find(filter['position'])<0:
                continue
            text = self.ui.tableWidget.item(row,self.col_names['厂商']).text()
            if text != '' and text.find(filter['supplier'])<0:
                continue
            text = self.ui.tableWidget.item(row,self.col_names['型号']).text()
            if text != '' and text.find(filter['model'])<0:
                continue
            text = self.ui.tableWidget.item(row,self.col_names['描述']).text()
            if text != '' and text.find(filter['info'])<0:
                continue
            self.ui.tableWidget.showRow(row)

        # self.signal.search_device.emit(filter)

